<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>电影列表</title>
</head>
<style>
    * {
        padding: 0;
        margin: 0;
        list-style: none;
        text-decoration: none;
    }
    
    body {
        background-color: grey;
    }
    
    table {
        border-collapse: collapse;
        width: 700px;
    }
    
    td,
    th {
        width: 200px;
        border: 1px solid black;
    }
    
    td {
        text-align: center;
    }
    
    .box {
        margin: 0 auto;
        margin-top: 100px;
        width: 800px;
        height: 2000px;
        background-color: white;
        padding: 30px;
    }
    
    table {
        margin: 0 auto;
        margin-top: 30px;
    }
    
    ul {
        margin-left: 100px;
        width: 300px;
    }
    
    ul li {
        float: left;
        padding-right: 20px;
    }
    
    button {
        padding-left: 5px;
        padding-right: 5px;
    }
</style>

<body>
    <div class="box">
        <h1>电影列表</h1>
        <table>
            <thead>
                <tr>
                    <th><input type="checkbox" class="select_all">作品名字</th>
                    <th>年份</th>
                    <th>评分</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>

            </tbody>
            <tfoot>
                <tr>
                    <th colspan=5>总共星星数:0</th>
                </tr>
            </tfoot>
        </table>
    </div>
</body>
<script>
    var film = [{
        name: "记忆大师",
        year: 2017,
        score: "4星",
    }, {
        name: "铁拳",
        year: 2015,
        score: "4星",
    }, {
        name: "摔跤吧！爸爸",
        year: 2015,
        score: "5星",
    }, {
        name: "速度与激情8",
        year: 2014,
        score: "5星",
    }, {
        name: "盗梦空间",
        year: 2002,
        score: "4星",
    }, {
        name: "富丽春景",
        year: 2005,
        score: "2星",
    }];
    window.onload = function() {
        var btnS = document.querySelector(".btn_s");
        var btnZ = document.querySelector(".btn_z");
        var tbody = document.querySelector("tbody");
        var selectAll = document.querySelector(".select_all");
        var priceAll = document.querySelector("tfoot th");
        var showList = function() {
            var content = "";
            for (var i = 0; i < film.length; i++) {
                content = content + "<tr index=" + i + "><td><input type='checkbox' class='select'>" + film[i].name + "</td><td>" + film[i].year + "</td><td><button class='btn_reduce'>-</button><span>" + film[i].score + "</span><button class='btn_add'>+</button></td><td><button class='btn_delete'>删除</button><tr></tr>";
            }
            tbody.innerHTML = content;
            var selects = document.querySelectorAll(".select");
            var btnRed = document.querySelectorAll(".btn_reduce");
            var btnAdd = document.querySelectorAll(".btn_add");
            var btnDelete = document.querySelectorAll(".btn_delete");

            for (var i = 0; i < btnDelete.length; i++) {
                btnDelete[i].addEventListener("click", function(e) {
                    var td = this.parentNode;
                    var tr = this.parentNode.parentNode;
                    var index = tr.getAttribute("index");
                    tbody.removeChild(tr);
                    film.splice(index, 1);
                })
            }

            for (var i = 0; i < btnRed.length; i++) {
                btnRed[i].addEventListener("click", function(e) {
                    var td = this.parentNode;
                    var tr = this.parentNode.parentNode;
                    var children = this.parentNode.children;
                    var spans = Number(children[1].innerText[0])
                    if (spans > 0) {
                        spans = spans - 1;
                    }
                    children[1].innerText = spans + "星";
                    var index = tr.getAttribute("index");
                    film[index].score = spans + "星";

                    var checkbox = tr.children[0].querySelector("input[type='checkbox']");
                    var nowScore = Number(priceAll.innerText.slice(6));
                    if (checkbox.checked == true) {
                        priceAll.innerHTML = "总共星星数:" + (nowScore - 1);
                    }

                })
            }

            for (var i = 0; i < btnAdd.length; i++) {
                btnAdd[i].addEventListener("click", function(e) {
                    var td = this.parentNode;
                    var tr = this.parentNode.parentNode;
                    var children = this.parentNode.children;
                    var spans = Number(children[1].innerText[0])
                    if (spans < 5) {
                        spans = spans + 1;
                        var s = 1;
                    } else {
                        s = 0;
                    }
                    children[1].innerText = spans + "星";
                    var index = tr.getAttribute("index");
                    film[index].score = spans + "星";

                    var checkbox = tr.children[0].querySelector("input[type='checkbox']");
                    var nowScore = Number(priceAll.innerText.slice(6));
                    if (checkbox.checked == true) {
                        priceAll.innerHTML = "总共星星数:" + (nowScore + s);
                    }

                })
            }
            // 勾选
            selectAll.addEventListener("click", function(e) {
                var tScore = 0;
                if (selectAll.checked == true) {
                    for (var i = 0; i < selects.length; i++) {
                        selects[i].checked = true;
                        var score = Number(film[i].score[0]);
                        tScore = tScore + score;
                    }
                }
                if (selectAll.checked == false) {
                    for (var i = 0; i < selects.length; i++) {
                        selects[i].checked = false;
                    }
                }
                priceAll.innerHTML = "总共星星数:" + tScore;
            })
            for (var i = 0; i < selects.length; i++) {
                selects[i].addEventListener("click", function(e) {
                    if (this.checked == false) {
                        selectAll.checked = false;
                    }
                })
            }
            for (var i = 0; i < selects.length; i++) {
                selects[i].addEventListener("click", function(e) {
                    var td = this.parentNode;
                    var tr = this.parentNode.parentNode;
                    var index = tr.getAttribute("index");
                    var totalScore = Number(film[index].score[0]);
                    var nowScore = Number(priceAll.innerText.slice(6));
                    if (this.checked == true) {
                        priceAll.innerHTML = "总共星星数:" + (nowScore + totalScore);
                    } else {
                        priceAll.innerHTML = "总共星星数:" + (nowScore - totalScore);
                    }
                })
            }



        }
        showList();
    }
</script>

</html>